package com.example.skillcinema.adapters

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.skillcinema.databinding.StaffItemBinding
import com.example.skillcinema.db.models.Staff
import com.example.skillcinema.viewholders.StaffViewHolder

class StaffRecyclerViewAdapter(val onClick: (id: Int) -> Unit) :
    RecyclerView.Adapter<StaffViewHolder>() {
    private var values: List<Staff> = listOf()
    fun setData(list: List<Staff>) {
        values = list
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): StaffViewHolder = StaffViewHolder(
        StaffItemBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

    override fun onBindViewHolder(
        holder: StaffViewHolder,
        position: Int
    ) {
        val item: Staff = values[position]
        with(holder.binding) {
            root.setOnClickListener {
                onClick(item.staffId)
            }
            Glide.with(staffImageView).load(item.posterUrl)
                .placeholder(android.R.drawable.progress_indeterminate_horizontal)
                .error(android.R.drawable.stat_notify_error).into(staffImageView)
            staffFullNameTextView.text = item.nameRu
            staffRoleTextView.text =
                if (item.professionKey == "ACTOR" && item.description != null) item.description else item.professionText
        }
    }

    override fun getItemCount(): Int = values.count()
}